// 回合结算
class roundEndGame extends eui.Component{
    private alphaBg:eui.Image;
    private bgContent:eui.Group;
    private title:eui.Image;
    private btn_share:eui.Image;
    private btn_player:eui.Image;
    private txt_player:eui.Image;
    private contentBg:egret.Sprite;
    private surplusCardBg:egret.Sprite;
    public PLAYER:string = "player";

    public constructor(){
        super();
        this.once(eui.UIEvent.COMPLETE,this.onComplete,this);
        this.skinName = "src/Game/gameRoom/end/roundEndGameSkin.exml";
    }
    private onComplete(){
        this.alphaBg.touchEnabled = true;
        this.contentBg = new egret.Sprite();
        this.addChild(this.contentBg);
        this.surplusCardBg = new egret.Sprite();
        this.addChild(this.surplusCardBg);
        this.once(egret.Event.REMOVED_FROM_STAGE,this.onRemove,this);
        StageData.MainObj.addEventListener(Events.STAGE_SIZE,this.onMainSize,this);
        this.btn_share.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onShare,this);
        this.btn_player.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onPlayer,this);
        if(GameData.roundNo){//回放
            this.btn_player.visible = false;
            this.txt_player.visible = false;
        }
        this.onMainSize();
    }
    private onRemove(){
        StageData.MainObj.removeEventListener(Events.STAGE_SIZE,this.onMainSize,this);
        this.btn_share.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onShare,this);
        this.btn_player.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onPlayer,this);
    }

    private onMainSize(){
        this.alphaBg.width = StageData.W;
        this.alphaBg.height = StageData.H;
        this.bgContent.x = (this.alphaBg.width - this.bgContent.width)/2;
        this.bgContent.y = (this.alphaBg.height - this.bgContent.height)/2;
        this.contentBg.x = this.bgContent.x+15;
        this.contentBg.y = this.bgContent.y+8;

        this.surplusCardBg.x = this.contentBg.x;
        this.surplusCardBg.y = this.contentBg.y+410;
    }

    private onShare(){
        //alert("分享战绩");
        StageData.GameRoot.dispatchEventWith(GameEvent.CLEW_SHARE);
    }
    private onPlayer(){
        //alert("继续游戏");
        this.dispatchEventWith(this.PLAYER);
    }
    //设置数据内容
    public setContent(msg){        
        console.log('endGame', msg)
        // StageData.soundObj.soundSinglePlay("bg_hu");
        var mgs_data:any = msg;     
        
        for(var i=this.contentBg.numChildren;i>0;i--){
            this.contentBg.removeChildAt(i-1);
        }        
        mgs_data.settlements = mgs_data.settlements.sort((a, b) => a.seatIndex > b.seatIndex)
        for(let i=0;i<mgs_data.settlements.length;i++){
            let roundobj:roundEndGameObj = new roundEndGameObj();
            let lsData:any = mgs_data.settlements[i];
            const {hu, dianpao, imageUrl, userName, handPoker, winPoker, huPoker, zhaMaList, winScore, bankerIndex, seatIndex, zhaMaConut, suitPatterns} = lsData
            if (hu === true) {
                var isZimo = dianpao === -1
                if (!isZimo) {
                    var fangpaode = mgs_data.settlements.find(settle => settle.seatIndex === dianpao)
                    console.log('fangpaode', fangpaode);
                    fangpaode.showText = '放炮'
                    lsData.showText = '点炮'
                } else {
                    lsData.showText = '自摸'
                }
            }
            // roundobj.setContent(lsData.headUrl,lsData.nickname,lsData.handList,lsData.actionList,lsData.winCard,mgs_data.zhaMaList,lsData.point,mgs_data.bankerIndex,lsData.seatIndex,lsData.zhaMa);

            console.log('mgs_data', mgs_data);
            const zhamaList = mgs_data.zhaMaIndex.filter(zhama => zhama.index === lsData.seatIndex)
            // setTimeout(() => {
            //     roundobj.setContent(lsData.headUrl,lsData.nickname,lsData.handList,lsData.actionList,lsData.winCard,zhamaList,lsData.point,mgs_data.bankerIndex,lsData.seatIndex,lsData.zhaMa, lsData.suitPatterns, lsData.showText, lsData.winCards);
            //      // roundobj.setContent(lsData, zhamaList, mgs_data.bankerIndex);                                    
            //     this.contentBg.addChild(roundobj);
            //     roundobj.y = 100*i;
            // }, 0)
            // roundobj.setContent(lsData, zhamaList, mgs_data.bankerIndex);                                    
            roundobj.setContent(lsData.headUrl,lsData.nickname,lsData.handList,lsData.actionList,lsData.winCard,zhamaList,lsData.point,mgs_data.bankerIndex,lsData.seatIndex,lsData.zhaMa, lsData.suitPatterns, lsData.showText, lsData.winCards);
            this.contentBg.addChild(roundobj);
            roundobj.y = 100*i;
            if(lsData.seatIndex == GameData.mySeatIndex){
                if(lsData.point<0){
                    this.title.source = "endtitle2_png";
                }else{
                    this.title.source = "endtitle_png";
                }
            }
        }
         
        let lsLength:number = mgs_data.remainList.length;
        for(var n:number=0;n<lsLength;n++){    
            var pokerMc = new handPoker();            
                pokerMc.setContent(mgs_data.remainList[n].suitType+"_"+mgs_data.remainList[n].count+"_png");
                pokerMc.scaleX = pokerMc.scaleY = 0.47;
                this.surplusCardBg.addChild(pokerMc);
                pokerMc.x = 15+(37.5*((n)%29));
                pokerMc.y = (Math.floor((n)/29)*(50));
        }
        this.onMainSize();
    }
}  

/*
回合结束:{"bankerIndex":0,"commandCode":20005,"countdownSecond":10,"secure":false,
"settlements":[{"actionList":[{"actionCard":{"cardIndex":12,"count":2,"suitType":"S"},"actionCode":2,"actionIndex":3,"actionType":"PENG"},{"actionCard":{"cardIndex":29,"count":9,"suitType":"T"},"actionCode":2,"actionIndex":1,"actionType":"PENG"}],

"handList":[{"cardIndex":24,"count":4,"suitType":"T"},{"cardIndex":24,"count":4,"suitType":"T"},{"cardIndex":24,"count":4,"suitType":"T"},{"cardIndex":18,"count":8,"suitType":"S"},{"cardIndex":19,"count":9,"suitType":"S"},{"cardIndex":19,"count":9,"suitType":"S"},{"cardIndex":18,"count":8,"suitType":"S"}],

"headUrl":"http://wx.qlogo.cn/mmopen/vi_32/JU6qGkMcWsLibvIqeA4Z1X7XhGibVT7zMibibrEeEEv9PkL83puWgKDH4DygPX2q9BQtwoN5TicXkyIGx9qFwEZNPEg/0","point":2,"presentPoint":2,"seatIndex":0},{"actionList":[{"actionCard":{"cardIndex":11,"count":1,"suitType":"S"},"actionCode":2,"actionIndex":0,"actionType":"PENG"},{"actionCard":{"cardIndex":6,"count":6,"suitType":"W"},"actionCode":2,"actionIndex":3,"actionType":"PENG"}],"handList":[{"cardIndex":31,"count":-1,"suitType":"Z"},{"cardIndex":3,"count":3,"suitType":"W"},{"cardIndex":3,"count":3,"suitType":"W"},{"cardIndex":14,"count":4,"suitType":"S"},{"cardIndex":25,"count":5,"suitType":"T"},{"cardIndex":25,"count":5,"suitType":"T"},{"cardIndex":31,"count":-1,"suitType":"Z"},{"cardIndex":31,"count":-1,"suitType":"Z"}],

"headUrl":"http://wx.qlogo.cn/mmopen/vi_32/VO9XZrCJy6ZKID2FhEz9MvlPogbibUNCcBg6NY75H3KHVckbf1eHWqCqpcMcJs0f58dlFFgzL85BQ20Ih1gxemA/0","point":2,"presentPoint":2,"seatIndex":2,"suitPatterns":[2],"winCard":{"cardIndex":31,"count":-1,"suitType":"Z"}},{"actionList":[{"actionCard":{"cardIndex":7,"count":7,"suitType":"W"},"actionCode":2,"actionIndex":0,"actionType":"PENG"}],"handList":[{"cardIndex":22,"count":2,"suitType":"T"},{"cardIndex":25,"count":5,"suitType":"T"},{"cardIndex":26,"count":6,"suitType":"T"},{"cardIndex":17,"count":7,"suitType":"S"},{"cardIndex":18,"count":8,"suitType":"S"},{"cardIndex":19,"count":9,"suitType":"S"},{"cardIndex":31,"count":-1,"suitType":"Z"},{"cardIndex":26,"count":6,"suitType":"T"},{"cardIndex":23,"count":3,"suitType":"T"},{"cardIndex":18,"count":8,"suitType":"S"}],

"headUrl":"http://wx.qlogo.cn/mmopen/vi_32/P1gYnyZB9azUrDicY7Iia0DxIm8LEMUNoQ7XAk3N7PjTqAJsCRXRiayVeQkHhMzgamSpmMiaSfTnX53faTSqNSIdCA/0","point":2,"presentPoint":2,"seatIndex":1,"winCard":{"cardIndex":18,"count":8,"suitType":"S"}},{"actionList":[{"actionCard":{"cardIndex":16,"count":6,"suitType":"S"},"actionCode":2,"actionIndex":0,"actionType":"PENG"}],"handList":[{"cardIndex":1,"count":1,"suitType":"W"},{"cardIndex":21,"count":1,"suitType":"T"},{"cardIndex":2,"count":2,"suitType":"W"},{"cardIndex":3,"count":3,"suitType":"W"},{"cardIndex":15,"count":5,"suitType":"S"},{"cardIndex":15,"count":5,"suitType":"S"},{"cardIndex":28,"count":8,"suitType":"T"},{"cardIndex":28,"count":8,"suitType":"T"},{"cardIndex":21,"count":1,"suitType":"T"},{"cardIndex":8,"count":8,"suitType":"W"}],

"headUrl":"http://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83epjEPY237UPeHeXoSCG9RxTr1W9Ip1CPzTL3fjyGuvXsosDwppItnSGicfZNJTRujjasolbW0HGaPQ/0","point":2,"presentPoint":2,"seatIndex":3}],"step":55,"version":"0.1","zhaMaList":[{"card":{"cardIndex":15,"count":5,"suitType":"S"},"win":true},{"card":{"cardIndex":8,"count":8,"suitType":"W"},"win":false}]}*/
/*"bankerIndex":0,"commandCode":20005,"countdownSecond":10,"secure":false,"settlements":[{"actionList":[{"actionCard":{"cardIndex":12,"count":2,"suitType":"S"},"actionCode":2,"actionIndex":3,"actionType":"PENG"},{"actionCard":{"cardIndex":29,"count":9,"suitType":"T"},"actionCode":2,"actionIndex":1,"actionType":"PENG"}],"handList":[{"cardIndex":24,"count":4,"suitType":"T"},{"cardIndex":24,"count":4,"suitType":"T"},{"cardIndex":24,"count":4,"suitType":"T"},{"cardIndex":18,"count":8,"suitType":"S"},{"cardIndex":19,"count":9,"suitType":"S"},{"cardIndex":19,"count":9,"suitType":"S"},{"cardIndex":18,"count":8,"suitType":"S"}],"nickname":"noName","headUrl":"http://wx.qlogo.cn/mmopen/vi_32/JU6qGkMcWsLibvIqeA4Z1X7XhGibVT7zMibibrEeEEv9PkL83puWgKDH4DygPX2q9BQtwoN5TicXkyIGx9qFwEZNPEg/0","point":2,"presentPoint":2,"seatIndex":0},{"actionList":[{"actionCard":{"cardIndex":11,"count":1,"suitType":"S"},"actionCode":2,"actionIndex":0,"actionType":"PENG"},{"actionCard":{"cardIndex":6,"count":6,"suitType":"W"},"actionCode":2,"actionIndex":3,"actionType":"PENG"}],"handList":[{"cardIndex":31,"count":-1,"suitType":"Z"},{"cardIndex":3,"count":3,"suitType":"W"},{"cardIndex":3,"count":3,"suitType":"W"},{"cardIndex":14,"count":4,"suitType":"S"},{"cardIndex":25,"count":5,"suitType":"T"},{"cardIndex":25,"count":5,"suitType":"T"},{"cardIndex":31,"count":-1,"suitType":"Z"},{"cardIndex":31,"count":-1,"suitType":"Z"}],"nickname":"noName","headUrl":"http://wx.qlogo.cn/mmopen/vi_32/VO9XZrCJy6ZKID2FhEz9MvlPogbibUNCcBg6NY75H3KHVckbf1eHWqCqpcMcJs0f58dlFFgzL85BQ20Ih1gxemA/0","point":2,"presentPoint":2,"seatIndex":2,"suitPatterns":[2],"winCard":{"cardIndex":31,"count":-1,"suitType":"Z"}},{"actionList":[{"actionCard":{"cardIndex":7,"count":7,"suitType":"W"},"actionCode":2,"actionIndex":0,"actionType":"PENG"}],"handList":[{"cardIndex":22,"count":2,"suitType":"T"},{"cardIndex":25,"count":5,"suitType":"T"},{"cardIndex":26,"count":6,"suitType":"T"},{"cardIndex":17,"count":7,"suitType":"S"},{"cardIndex":18,"count":8,"suitType":"S"},{"cardIndex":19,"count":9,"suitType":"S"},{"cardIndex":31,"count":-1,"suitType":"Z"},{"cardIndex":26,"count":6,"suitType":"T"},{"cardIndex":23,"count":3,"suitType":"T"},{"cardIndex":18,"count":8,"suitType":"S"}],"nickname":"noName","headUrl":"http://wx.qlogo.cn/mmopen/vi_32/P1gYnyZB9azUrDicY7Iia0DxIm8LEMUNoQ7XAk3N7PjTqAJsCRXRiayVeQkHhMzgamSpmMiaSfTnX53faTSqNSIdCA/0","point":2,"presentPoint":2,"seatIndex":1,"winCard":{"cardIndex":18,"count":8,"suitType":"S"}},{"actionList":[{"actionCard":{"cardIndex":16,"count":6,"suitType":"S"},"actionCode":2,"actionIndex":0,"actionType":"PENG"}],"handList":[{"cardIndex":1,"count":1,"suitType":"W"},{"cardIndex":21,"count":1,"suitType":"T"},{"cardIndex":2,"count":2,"suitType":"W"},{"cardIndex":3,"count":3,"suitType":"W"},{"cardIndex":15,"count":5,"suitType":"S"},{"cardIndex":15,"count":5,"suitType":"S"},{"cardIndex":28,"count":8,"suitType":"T"},{"cardIndex":28,"count":8,"suitType":"T"},{"cardIndex":21,"count":1,"suitType":"T"},{"cardIndex":8,"count":8,"suitType":"W"}],"nickname":"noName","headUrl":"http://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83epjEPY237UPeHeXoSCG9RxTr1W9Ip1CPzTL3fjyGuvXsosDwppItnSGicfZNJTRujjasolbW0HGaPQ/0","point":2,"presentPoint":2,"seatIndex":3}],"step":55,"version":"0.1","zhaMaList":[{"card":{"cardIndex":15,"count":5,"suitType":"S"},"win":true},{"card":{"cardIndex":8,"count":8,"suitType":"W"},"win":false}]};*/